module RegexpBuilder

    def pattern(pattern)
        return /#{pattern}/
    end

#    Anchors
    def start_with(pattern)
        "\\A" + pattern
    end

    def end_with(pattern)
        pattern + "\\z"
    end

    def end_with_if_ignore_last_new_line(pattern)
        pattern + "\\Z"
    end

    def line_start_with(pattern)
        "^" + pattern
    end

    def line_end_with(pattern)
        pattern + "$"
    end

    def word_boundary(pattern)
        "\\b" + pattern
    end

    def nonword_boundary(pattern)
        "\\B" + pattern
    end


#    Character classes
    def either(pattern)
        "[" + pattern + "]"
    end

    def neither(pattern)
        "[^" + pattern + "]"
    end

    def digit
        "\\d"
    end

    def non_digit
        "\\D"
    end

    def space
        "\\s"
    end

    def non_space
        "\\S"
    end

    def word_char
        "\\w"
    end

    def non_word_char
        "\\W"
    end

    def any
        "."
    end

    def literal(pattern)
        Regexp.escape(pattern)
    end


#    repeat
    def repeat_any_times(pattern, mode=:greedy)
        pattern + (mode == :non_greedy ? "*?" : "*")
    end

    def repeat(pattern, least, most=-1, mode=:greedy)
        return pattern + "{" + least.to_s + "}" if most == -1
        return pattern + "{" + least.to_s + "," + most.to_s + "}" if mode == :greedy
        return pattern + "{" + least.to_s + "," + most.to_s + "}?"
    end

    def at_least_once(pattern, mode=:greedy)
        pattern + (mode == :non_greedy ? "+?" : "+")
    end

    def at_most_once(pattern)
        pattern + "?"
    end

    def at_least(times, pattern)
        pattern + "{" + times.to_s + ",}"
    end


#    Alternation
    def one_of(*patterns)
        "#{patterns.join '|' }"
    end


#    group
    def group(pattern)
        "(" + pattern + ")"
    end
    
end